package com.lswms.modules.lswms.service.impl.check;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.groovy.runtime.typehandling.BigDecimalMath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.page.PageData;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.Constant.CheckConstant;
import com.lswms.modules.lswms.Constant.TaskConstant;
import com.lswms.modules.lswms.dao.base.LocationDao;
import com.lswms.modules.lswms.dao.base.SkuDao;
import com.lswms.modules.lswms.dao.check.CheckDao;
import com.lswms.modules.lswms.dao.check.CheckdetailDao;
import com.lswms.modules.lswms.dao.stock.InventoryDao;
import com.lswms.modules.lswms.dao.task.TaskDao;
import com.lswms.modules.lswms.dto.check.CheckdetailDTO;
import com.lswms.modules.lswms.entity.base.LocationEntity;
import com.lswms.modules.lswms.entity.base.SkuEntity;
import com.lswms.modules.lswms.entity.check.CheckEntity;
import com.lswms.modules.lswms.entity.check.CheckdetailEntity;
import com.lswms.modules.lswms.entity.receipt.ReceiptdetaillistEntity;
import com.lswms.modules.lswms.entity.stock.InventoryEntity;
import com.lswms.modules.lswms.entity.task.TaskEntity;
import com.lswms.modules.lswms.service.base.SequenceService;
import com.lswms.modules.lswms.service.check.CheckdetailService;
import com.lswms.modules.security.user.SecurityUser;

/**
 * 盘点明细
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-07-08
 */
@Service
public class CheckdetailServiceImpl extends CrudServiceImpl<CheckdetailDao, CheckdetailEntity, CheckdetailDTO>
		implements CheckdetailService {

	@Autowired
	CheckDao checkDao;

	@Autowired
	SkuDao skuDao;

	@Autowired
	LocationDao locationDao;

	@Autowired
	InventoryDao inventoryDao;

	@Autowired
	TaskDao taskDao;

	@Autowired
	SequenceService sequenceService;

	@Override
	public QueryWrapper<CheckdetailEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");
		String checkkey = (String) params.get("checkkey");
		String status = (String) params.get("status");
		String sku = (String) params.get("sku");
		String lot = (String) params.get("lot");
		String location = (String) params.get("location");
		String tray = (String) params.get("tray");
		String box = (String) params.get("box");
		String descr = (String) params.get("descr");

		QueryWrapper<CheckdetailEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "lswms_checkdetail.id", id);
		wrapper.eq(StringUtils.isNotBlank(checkkey), "lswms_checkdetail.checkkey", checkkey);
		wrapper.eq(StringUtils.isNotBlank(status), "lswms_checkdetail.status", status);
		wrapper.eq(StringUtils.isNotBlank(sku), "lswms_checkdetail.sku", sku);
		wrapper.eq(StringUtils.isNotBlank(lot), "lswms_checkdetail.lot", lot);
		wrapper.eq(StringUtils.isNotBlank(location), "lswms_checkdetail.location", location);
		wrapper.eq(StringUtils.isNotBlank(tray), "lswms_checkdetail.tray", tray);
		wrapper.eq(StringUtils.isNotBlank(box), "lswms_checkdetail.box", box);
		wrapper.like(StringUtils.isNotBlank(descr), "lswms_sku.descr", descr);

		wrapper.orderByDesc("checkkey");
		wrapper.orderByAsc("line");
		return wrapper;
	}

	@Override
	public PageData<CheckdetailEntity> pageBySql(Map<String, Object> params) {
		IPage<CheckdetailEntity> page = baseDao.selectPageBySql(getPage(params, null, false), getWrapper(params));
		return getPageData(page, CheckdetailEntity.class);
	}

	@Override
	public List<CheckdetailEntity> listBySql(Map<String, Object> params) {
		List<CheckdetailEntity> list = baseDao.listBySql( getWrapper(params));
		return list;
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void editUpdate(List<CheckdetailDTO> dtolist) {
		// TODO Auto-generated method stub
		String user = SecurityUser.getUser().getUsername();

		for (CheckdetailDTO checkdetailDTO : dtolist) {
			UpdateWrapper<CheckdetailEntity> updateWrapper = new UpdateWrapper<CheckdetailEntity>();
			updateWrapper.eq("checkkey", checkdetailDTO.getCheckkey());
			updateWrapper.eq("line", checkdetailDTO.getLine());

			CheckdetailEntity checkdetailEntity = new CheckdetailEntity();
			checkdetailEntity.setFirstqty(checkdetailDTO.getFirstqty());
			checkdetailEntity.setSecondqty(checkdetailDTO.getSecondqty());

			checkdetailEntity.setFirstuser(user);
			checkdetailEntity.setSeconduser(user);

			checkdetailEntity.setFirsttime(new Date());
			checkdetailEntity.setSecondaddtime(new Date());
			baseDao.update(checkdetailEntity, updateWrapper);
		}
	}

	@Override
	public List<CheckdetailEntity> getByTray(String tray) {
		QueryWrapper<CheckdetailEntity> queryWrapper = new QueryWrapper<CheckdetailEntity>();
		queryWrapper.eq("lswms_checkdetail.tray", tray);
		queryWrapper.le("lswms_checkdetail.status", CheckConstant.CHECK_STATUS_9);
		queryWrapper.notIn("lswms_checkdetail.status","9");

		queryWrapper.orderByDesc("lswms_checkdetail.checkkey");
		queryWrapper.orderByAsc("lswms_checkdetail.line");
		return baseDao.selectListBySql(queryWrapper);
	}

	@Override
	public Map<String, Object> getcheckdetailKeyAndLocationByTray(String tray) {
		// 查询当前盘点信息
		QueryWrapper<CheckEntity> queryWrapper = new QueryWrapper<CheckEntity>();
//		queryWrapper.eq("lswms_check.tray", tray);
		queryWrapper.lt("lswms_check.status", CheckConstant.CHECK_STATUS_9);

		queryWrapper.orderByDesc("lswms_check.checkkey");

		String checkkey = "";
		List<CheckEntity> list = checkDao.selectList(queryWrapper);
		if (list.size() > 0) {
			checkkey = list.get(0).getCheckkey();
		}

		String location = "";
		// 查询托盘所在的库位信息
		if (StringUtils.isNotEmpty(checkkey)) {
			QueryWrapper<CheckdetailEntity> checkdetailQuery = new QueryWrapper<CheckdetailEntity>();
			checkdetailQuery.eq("lswms_checkdetail.tray", tray);
			checkdetailQuery.eq("lswms_checkdetail.checkkey", checkkey);
			checkdetailQuery.le("lswms_checkdetail.status", CheckConstant.CHECK_STATUS_9);

			checkdetailQuery.orderByDesc("lswms_checkdetail.checkkey");
			checkdetailQuery.orderByAsc("lswms_checkdetail.line");

			List<CheckdetailEntity> checkdetailList = baseDao.selectList(checkdetailQuery);
			if (checkdetailList.size() > 0) {
				location = checkdetailList.get(0).getLocation();
			}

		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("checkkey", checkkey);
		map.put("location", location);

		return map;
	}

	@Override
	public String saveCheckdetailIn(CheckdetailEntity dto) {
		String checkkey = dto.getCheckkey();
		String sku = dto.getSku();
		String location = dto.getLocation();
		String tray = dto.getTray();
		String box = dto.getBox();
		BigDecimal firstQty = dto.getFirstqty();
		BigDecimal secondQty = dto.getSecondqty();

		if (StringUtils.isBlank(checkkey)) {
			return "不存在盘点号，保存失败";
		}
		if (StringUtils.isBlank(sku)) {
			return "物料编码不存在，保存失败";
		}
		if (StringUtils.isBlank(location)) {
			return "库位编码不能为空，保存失败";
		}
		if (StringUtils.isBlank(box)) {
			return "容器不能为空，保存失败";
		}
		if (firstQty.compareTo(BigDecimal.ZERO) < 0) {
			return "初盘数量不能为负数，保存失败";
		}
		if (secondQty.compareTo(BigDecimal.ZERO) < 0) {
			return "复盘数量不能为负数，保存失败";
		}
		// 校验盘点号是否为完成
		QueryWrapper<CheckEntity> queryWrapper = new QueryWrapper<CheckEntity>();
		queryWrapper.eq("lswms_check.checkkey", checkkey);

		CheckEntity checkEntity = checkDao.selectOne(queryWrapper);
		if (checkEntity == null) {
			return "不存在盘点号，保存失败";
		}
		Integer status = checkEntity.getStatus();
		if (status == 9) {
			return "盘点号已过账，保存失败";
		}
		if (status == 0) {
			return "盘点号未生成明细，保存失败";
		}

		// 校验sku信息
		QueryWrapper<SkuEntity> skuQuery = new QueryWrapper<SkuEntity>();
		skuQuery.eq("lswms_sku.sku", sku);
		SkuEntity skuEntity = skuDao.selectOne(skuQuery);
		if (skuEntity == null) {
			return "物料编码不存在，保存失败";
		}

		String isuniquecode = skuEntity.getIsuniquecode() ;
		if(StringUtils.isNotBlank(isuniquecode)&& isuniquecode.equals("1")) {
			String uniquecodeBuild = dto.getUniquecodes() ;
			String[] uniquecodeBuilds = uniquecodeBuild.split("\n");

			List<String> uniquecodes = new LinkedList<>();
			for (String uniquecode : uniquecodeBuilds) {
				uniquecodes.add(uniquecode);
			}
			long count = uniquecodes.stream().distinct().count();
			if (uniquecodes.size() != count) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return "请不要输入重复唯一码";
			}
			BigDecimal secondqty = dto.getSecondqty();
			Number number = BigDecimalMath.abs(secondqty);

			int i = number.intValue();
			if (i != uniquecodes.size()) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return "唯一码的数量不等于复盘数量";
			}
//			for (String uniquecode : uniquecodes) {
//				if (uniquecodeDao.checkUniquecode(uniquecode) > 0) {
//					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//					return "此唯一码已存在:" + uniquecode;
//				}
//			}
		}
		
		// 校验库位信息
		QueryWrapper<LocationEntity> locationQuery = new QueryWrapper<LocationEntity>();
		locationQuery.eq("lswms_location.location", location);
		LocationEntity locationEntity = locationDao.selectOne(locationQuery);
		if (locationEntity == null) {
			return "库位编码不存在，保存失败";
		}

		// 校验托盘上的库存信息
//		QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
//		inventoryQuery.eq("lswms_inventory.tray", tray);
//		inventoryQuery.eq("lswms_inventory.box", box);
//
//		List<InventoryEntity> inventorylist = inventoryDao.selectList(inventoryQuery);
//		for (InventoryEntity inventoryEntity : inventorylist) {
//			BigDecimal qty = inventoryEntity.getQty();
//			BigDecimal putqty = inventoryEntity.getPutqty();
//			BigDecimal pickqty = inventoryEntity.getPickqty();
//			String skuInventory = inventoryEntity.getSku();
//			String boxInventory = inventoryEntity.getBox();
//			String locationInventory = inventoryEntity.getLocation();
//
//			if (qty.compareTo(BigDecimal.ZERO) <= 0 && putqty.compareTo(BigDecimal.ZERO) <= 0
//					&& pickqty.compareTo(BigDecimal.ZERO) <= 0) {
//				continue;
//			}
//
////			if (!location.equals(locationInventory)) {
////				return "录入的库位编码与托盘实际的库位不一致，保存失败";
////			}
//
//			if (boxInventory.equals(box)) {
//				if (!sku.equals(skuInventory)) {
//					return "录入物料与格的实际物料不一致，保存失败";
//				}
//			}
//		}
		// 查询当前盘点任务的记录
		QueryWrapper<CheckdetailEntity> checkdetailQuery = new QueryWrapper<CheckdetailEntity>();
		checkdetailQuery.eq("checkkey", checkkey);
		checkdetailQuery.eq("tray", tray);
		checkdetailQuery.eq("box", box);
		checkdetailQuery.eq("secondqty", 0);
		Integer count = baseDao.selectCount(checkdetailQuery)	;
		if(count>0) {
			return "箱:" + tray + " 格："+box +"已被占用，请更换其它格号";
		}
		
//		List<CheckdetailEntity> checkdetaillist = baseDao.selectList(checkdetailQuery);
//		for (CheckdetailEntity checkdetailEntity : checkdetaillist) {
//			String skuCheckdetail = checkdetailEntity.getSku();
//			if (!sku.equals(skuCheckdetail)) {
//				return "本次盘点记录中容器已有其他物料，保存失败";
//			}
//		}

		// 新增盘点记录

		// 获取盘点行号
		int line = baseDao.getNextCheckdetailLine(checkkey);

		CheckdetailEntity checkdetailEntityIn = new CheckdetailEntity();
		checkdetailEntityIn.setCheckkey(checkkey);
		checkdetailEntityIn.setLine(line);
		checkdetailEntityIn.setSku(sku);
		checkdetailEntityIn.setLocation(location);
		checkdetailEntityIn.setTray(tray);
		checkdetailEntityIn.setBox(box);
		checkdetailEntityIn.setQty(BigDecimal.ZERO);
		checkdetailEntityIn.setStatus(CheckConstant.CHECK_STATUS_5);
		checkdetailEntityIn.setFirstqty(firstQty);
		checkdetailEntityIn.setFirstuser(SecurityUser.getUser().getUsername());
		checkdetailEntityIn.setFirsttime(new Date());
		checkdetailEntityIn.setSecondqty(secondQty);
		checkdetailEntityIn.setSeconduser(SecurityUser.getUser().getUsername());
		checkdetailEntityIn.setSecondaddtime(new Date());

		checkdetailEntityIn.setLot1(dto.getLot1());
		checkdetailEntityIn.setLot2(dto.getLot2());
		checkdetailEntityIn.setLot3(StringUtils.trimToEmpty(dto.getLot3()));
		checkdetailEntityIn.setLot4(StringUtils.trimToEmpty(dto.getLot4()));
		checkdetailEntityIn.setLot5(StringUtils.trimToEmpty(dto.getLot5()));
		checkdetailEntityIn.setLot6(StringUtils.trimToEmpty(dto.getLot6()));
		checkdetailEntityIn.setLot7(StringUtils.trimToEmpty(dto.getLot7()));
		checkdetailEntityIn.setLot8(StringUtils.trimToEmpty(dto.getLot8()));
		checkdetailEntityIn.setLot9(StringUtils.trimToEmpty(dto.getLot9()));
		checkdetailEntityIn.setLot10(StringUtils.trimToEmpty(dto.getLot10()));
		checkdetailEntityIn.setLot11(StringUtils.trimToEmpty(dto.getLot11()));
		checkdetailEntityIn.setLot12(StringUtils.trimToEmpty(dto.getLot12()));
		checkdetailEntityIn.setLot13(StringUtils.trimToEmpty(dto.getLot13()));
		checkdetailEntityIn.setLot14(StringUtils.trimToEmpty(dto.getLot14()));
		checkdetailEntityIn.setLot15(StringUtils.trimToEmpty(dto.getLot15()));
		checkdetailEntityIn.setUniquecodes(dto.getUniquecodes());
		baseDao.insert(checkdetailEntityIn);

		return null;

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String putCheckdetailSave2(List<CheckdetailEntity> dtos) {
		for (CheckdetailEntity checkdetailEntity : dtos) {
			BigDecimal firstQty = checkdetailEntity.getFirstqty();
			BigDecimal secondQty = checkdetailEntity.getSecondqty();
			String tray = checkdetailEntity.getTray();

			Integer status = checkdetailEntity.getStatus() ;
			if(status >= CheckConstant.CHECK_STATUS_7) {
				if(status == CheckConstant.CHECK_STATUS_7) {
					return "箱：" +tray +"生产回库任务，保存失败";
				}
				if(status == CheckConstant.CHECK_STATUS_9) {
					return "箱：" +tray +"已过帐，保存失败";
				}
				if(status == CheckConstant.CHECK_STATUS_15) {
					return "箱：" +tray +"盘点任务已取消，保存失败";
				}
			}

			if (firstQty.compareTo(BigDecimal.ZERO) < 0) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return "初盘量不能为负数，保存失败";
			}
			if (secondQty.compareTo(BigDecimal.ZERO) < 0) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return "复盘量不能为负数，保存失败";
			}

			String sku = checkdetailEntity.getSku();
			QueryWrapper<SkuEntity> skuQuery = new QueryWrapper<SkuEntity>();
			skuQuery.eq("lswms_sku.sku", sku);
			SkuEntity skuEntity = skuDao.selectOne(skuQuery);
			if (skuEntity == null) {
				return "物料编码不存在，保存失败";
			}

			String isuniquecode = skuEntity.getIsuniquecode() ;
			if(StringUtils.isNotBlank(isuniquecode)&& isuniquecode.equals("1")) {
				String uniquecodeBuild = checkdetailEntity.getUniquecodes() ;
				String[] uniquecodeBuilds =  StringUtils.isBlank(uniquecodeBuild) ? new String[0]:  uniquecodeBuild.split("\n");

				List<String> uniquecodes = new LinkedList<>();
				for (String uniquecode : uniquecodeBuilds) {
					uniquecodes.add(uniquecode);
				}
				long count = uniquecodes.stream().distinct().count();
				if (uniquecodes.size() != count) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return "请不要输入重复唯一码";
				}
				BigDecimal secondqty = checkdetailEntity.getSecondqty();
				Number number = BigDecimalMath.abs(secondqty);

				int i = number.intValue();
				if (i != uniquecodes.size()) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return "唯一码的数量不等于复盘数量";
				}
//				for (String uniquecode : uniquecodes) {
//					if (uniquecodeDao.checkUniquecode(uniquecode) > 0) {
//						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//						return "此唯一码已存在:" + uniquecode;
//					}
//				}
			}


			CheckdetailEntity checkdetailEntity1 = new CheckdetailEntity();
			checkdetailEntity1.setStatus(CheckConstant.CHECK_STATUS_7);
			checkdetailEntity1.setFirstqty(firstQty);
			checkdetailEntity1.setFirstuser(SecurityUser.getUser().getUsername());
			checkdetailEntity1.setSecondaddtime(new Date());
			checkdetailEntity1.setSecondqty(secondQty);
			checkdetailEntity1.setSeconduser(SecurityUser.getUser().getUsername());
			checkdetailEntity1.setSecondaddtime(new Date());

			UpdateWrapper<CheckdetailEntity> checkdetailUpdate = new UpdateWrapper<CheckdetailEntity>();
			checkdetailUpdate.eq("checkkey", checkdetailEntity.getCheckkey());
			checkdetailUpdate.eq("line", checkdetailEntity.getLine());

			baseDao.update(checkdetailEntity1, checkdetailUpdate);
		}

		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String putCheckdetailSave(List<CheckdetailEntity> dtos) {
		
		for (CheckdetailEntity checkdetailEntity : dtos) {
			BigDecimal firstQty = checkdetailEntity.getFirstqty();
			BigDecimal secondQty = checkdetailEntity.getSecondqty();
			String tray = checkdetailEntity.getTray();
			
			Integer status = checkdetailEntity.getStatus() ;
			if(status >= CheckConstant.CHECK_STATUS_7) {
				if(status == CheckConstant.CHECK_STATUS_7) {
					return "箱：" +tray +"生产回库任务，保存失败";
				}
				if(status == CheckConstant.CHECK_STATUS_9) {
					return "箱：" +tray +"已过帐，保存失败";
				}
				if(status == CheckConstant.CHECK_STATUS_15) {
					return "箱：" +tray +"盘点任务已取消，保存失败";
				}
			}
			
			if (firstQty.compareTo(BigDecimal.ZERO) < 0) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return "初盘量不能为负数，保存失败";
			}
			if (secondQty.compareTo(BigDecimal.ZERO) < 0) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return "复盘量不能为负数，保存失败";
			}
			
			String sku = checkdetailEntity.getSku();
			QueryWrapper<SkuEntity> skuQuery = new QueryWrapper<SkuEntity>();
			skuQuery.eq("lswms_sku.sku", sku);
			SkuEntity skuEntity = skuDao.selectOne(skuQuery);
			if (skuEntity == null) {
				return "物料编码不存在，保存失败";
			}
			
			String isuniquecode = skuEntity.getIsuniquecode() ;
			if(StringUtils.isNotBlank(isuniquecode)&& isuniquecode.equals("1")) {
				String uniquecodeBuild = checkdetailEntity.getUniquecodes() ;
				String[] uniquecodeBuilds =  StringUtils.isBlank(uniquecodeBuild) ? new String[0]:  uniquecodeBuild.split("\n");

				List<String> uniquecodes = new LinkedList<>();
				for (String uniquecode : uniquecodeBuilds) {
					uniquecodes.add(uniquecode);
				}
				long count = uniquecodes.stream().distinct().count();
				if (uniquecodes.size() != count) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return "请不要输入重复唯一码";
				}
				BigDecimal secondqty = checkdetailEntity.getSecondqty();
				Number number = BigDecimalMath.abs(secondqty);

				int i = number.intValue();
				if (i != uniquecodes.size()) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return "唯一码的数量不等于复盘数量";
				}
//				for (String uniquecode : uniquecodes) {
//					if (uniquecodeDao.checkUniquecode(uniquecode) > 0) {
//						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//						return "此唯一码已存在:" + uniquecode;
//					}
//				}
			}
			

			CheckdetailEntity checkdetailEntity1 = new CheckdetailEntity();
			checkdetailEntity1.setStatus(CheckConstant.CHECK_STATUS_7);
			checkdetailEntity1.setFirstqty(firstQty);
			checkdetailEntity1.setFirstuser(SecurityUser.getUser().getUsername());
			checkdetailEntity1.setSecondaddtime(new Date());
			checkdetailEntity1.setSecondqty(secondQty);
			checkdetailEntity1.setSeconduser(SecurityUser.getUser().getUsername());
			checkdetailEntity1.setSecondaddtime(new Date());

			UpdateWrapper<CheckdetailEntity> checkdetailUpdate = new UpdateWrapper<CheckdetailEntity>();
			checkdetailUpdate.eq("checkkey", checkdetailEntity.getCheckkey());
			checkdetailUpdate.eq("line", checkdetailEntity.getLine());

			baseDao.update(checkdetailEntity1, checkdetailUpdate);
		}

		// 下发回库任务
		for (CheckdetailEntity checkdetailEntity : dtos) {
			// 查询库位是否为立体库，若是立体库，产生回库任务
			String location = checkdetailEntity.getLocation();
			QueryWrapper<LocationEntity> locationQuery = new QueryWrapper<LocationEntity>();
			locationQuery.eq("location", location);
			LocationEntity locationEntity = locationDao.selectOne(locationQuery);

			if (locationEntity != null && locationEntity.getLocCategory().equals("STEREO")) {
				if (StringUtils.isNotBlank(checkdetailEntity.getLot())) {
					// 查询库存记录
//					QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
//					inventoryQuery.eq("location", checkdetailEntity.getLocation());
//					inventoryQuery.eq("lot", checkdetailEntity.getLot());
//					inventoryQuery.eq("tray", checkdetailEntity.getTray());
//					inventoryQuery.eq("box", checkdetailEntity.getBox());
//					inventoryQuery.eq("sku", checkdetailEntity.getSku());
//					InventoryEntity inventoryEntity = inventoryDao.selectOne(inventoryQuery);
//					if (inventoryEntity == null) {
//						return " 盘点单对应的库存记录不存在";
//					}

					// 添加回库任务
					TaskEntity taskEntity = new TaskEntity();
					String taskkey = sequenceService.getSequence("task");

					taskEntity.setTaskkey(taskkey);
					taskEntity.setTasktype(TaskConstant.TASK_CC);
					taskEntity.setPriority(0);
					taskEntity.setStatus(TaskConstant.TASK_0);
					taskEntity.setSku(checkdetailEntity.getSku());
					taskEntity.setQty(checkdetailEntity.getQty());
					taskEntity.setFromlocation(checkdetailEntity.getToloc());
					taskEntity.setTolocation(location);
					taskEntity.setFromtray(checkdetailEntity.getTray());
					taskEntity.setTotray(checkdetailEntity.getTray());
					taskEntity.setFrombox(checkdetailEntity.getBox());
					taskEntity.setTobox(checkdetailEntity.getBox());
					taskEntity.setFromlot(checkdetailEntity.getLot());
					taskEntity.setTolot(checkdetailEntity.getLot());
					taskEntity.setRemark("");
					taskDao.insert(taskEntity);
				}
			}
		}

		return null;
	}

	@Override
	public String putCheckdetailSaveUniquecodes(CheckdetailEntity dto) {
		CheckdetailEntity checkdetailEntity = new CheckdetailEntity();
		checkdetailEntity.setUniquecodes(dto.getUniquecodes());

		UpdateWrapper<CheckdetailEntity> update = new UpdateWrapper<CheckdetailEntity>();
		update.eq("checkkey", dto.getCheckkey());
		update.eq("line", dto.getLine());

		baseDao.update(checkdetailEntity, update);
		;

		return null;
	}

	@Override
	public String valideAndDelete(Long[] ids) {
		QueryWrapper<CheckdetailEntity> checkdetailQuery = new QueryWrapper<CheckdetailEntity>();
		checkdetailQuery.in("id", ids) ;
		List<CheckdetailEntity> list = baseDao.selectList(checkdetailQuery);
		
		for (CheckdetailEntity checkdetailEntity : list) {
			Integer status = checkdetailEntity.getStatus();
			
			if(status == CheckConstant.CHECK_STATUS_9) {
				return " 盘点已过账，无法删除" ;
			}
			if(status == CheckConstant.CHECK_STATUS_15) {
				return " 盘点已取消，无法删除" ;
			}
			
		}
		
		baseDao.deleteBatchIds(Arrays.asList(ids)) ;
		
		return null;
	}

	

}